import axios, { AxiosResponse, AxiosRequestConfig } from "axios";
import { message } from "ant-design-vue";
const [messageApi, contextHolder] = message.useMessage();
// 根据环境不同引入不同api地址
import { useMainStore } from "@/stores";

console.log("当前环境", import.meta.env.VITE_API_URL, import.meta.env.MODE);
const instance = axios.create({
  baseURL: import.meta.env.VITE_API_URL, // url = base url + request url
  timeout: 15000,
  withCredentials: false, // send cookies when cross-domain requests
  // headers: {
  // 	// clear cors
  // 	'Cache-Control': 'no-cache',
  // 	Pragma: 'no-cache'
  // }
});

// Request interceptors
instance.interceptors.request.use(
  (config) => {
    const stores = useMainStore();
    // 加载动画
    // if (config.loading) {
    // 	Toast.loading({
    // 		message: '加载中...',
    // 		forbidClick: true
    // 	})
    // }
    // 在此处添加请求头等，如添加 token
    // if (config.type !== 'mtToken') {
    if (stores.token) {
      console.log("config", config);
      config.headers["Authorization"] = stores.token;
    }
    // }

    // 添加语言
    // stores.mtLang && (config.headers["X-LANG"] = stores.mtLang);

    return config;
  },
  (error: any) => {
    Promise.reject(error);
  }
);

// Response interceptors
instance.interceptors.response.use(
  async (response: AxiosResponse) => {
    // await new Promise(resovle => setTimeout(resovle, 3000))
    // Toast.clear()
    const res = response.data;
    const mainStore = useMainStore();
    if (res.code != 200) {
      // token 过期
      if (res.code == -1) {
        mainStore.logout();
      }
      if (res.code === 401) {
        mainStore.logout();
        // 警告提示窗
        // return
      }
      if (res.code == 403) {
        // Dialog.alert({
        // 	title: '警告',
        // 	message: res.msg
        // })
        // return
      }
      if (res.code != 104002) {
        messageApi.error(res.msg);
      }
      // 若后台返回错误值，此处返回对应错误对象，下面 error 就会接收
      return Promise.reject(res);
    } else {
      // 注意返回值
      return response.data;
    }
  },
  (error: any) => {
    // Toast.clear()
    if (error && error.response) {
      console.error("🚀 ~ 出错的请求信息 :", error);

      const response = error.response;
      const mainStore = useMainStore();
      if (response.status === 401 && mainStore.isLogin) {
        mainStore.logout();
      }
      switch (error.response.status) {
        case 400:
          error.message = "请求错误(400)";
          break;
        case 401:
          error.message = "未授权,请登录(401)";
          mainStore.logout();
          break;
        case 403:
          error.message = "拒绝访问(403)";
          break;
        case 404:
          error.message = `请求地址出错: ${error.response.config.url}`;
          break;
        case 405:
          error.message = "请求方法未允许(405)";
          break;
        case 408:
          error.message = "请求超时(408)";
          break;
        case 500:
          error.message = "服务器内部错误(500)";
          break;
        case 501:
          error.message = "服务未实现(501)";
          break;
        case 502:
          error.message = "网络错误(502)";
          break;
        case 503:
          error.message = "服务不可用(503)";
          break;
        case 504:
          error.message = "网络超时(504)";
          break;
        case 505:
          error.message = "HTTP版本不受支持(505)";
          break;
        default:
          error.message = `连接错误: ${error.message}`;
      }
    } else {
      if (error.message == "Network Error") {
        error.message == "网络异常，请检查后重试！";
      }
      error.message = "连接到服务器失败，请联系管理员";
    }
    // Toast(error.message)
    return Promise.reject(error);
  }
);

export const postJSON = (url: string, data?: any): Promise<any> => {
  return instance.post(url, data, {
    headers: { "Content-Type": "application/json" },
  });
};

export default instance;
